package com.core.tag;

import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspWriter;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.core.dao.MenuDAO;
import com.core.model.Menus;
import com.core.model.Users;
import com.core.util.StringUtils;

/**
 * 获得子菜单
 * 
 * @author pt-chenqw
 *
 */
public class MenuSubItemTag extends RequestContextAwareTag {

	private static final long serialVersionUID = 1L;
	
	private String cssClass;
	
	private String cssId;
	
	private Long menuID;
	
	private MenuDAO menuDao;
	
	private String onclick;
	
	private String language;
	
	private String type;
	
	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getCssId() {
		return cssId;
	}

	public void setCssId(String cssId) {
		this.cssId = cssId;
	}

	public Long getMenuID() {
		return menuID;
	}

	public void setMenuID(Long menuID) {
		this.menuID = menuID;
	}
	
	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}
	
	public String getOnclick() {
		return onclick;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Override
	protected int doStartTagInternal() throws Exception {
		menuDao = (MenuDAO) this.getRequestContext().getWebApplicationContext().getBean("menuDAO");
		Map<String, Object> session = ServletActionContext.getContext().getSession();
		Users users = (Users) session.get("user");
		List<Menus> menus = menuDao.geSubMenusByParentID(menuID,users.getRoleID());
		JspWriter jspWriter = pageContext.getOut();
		StringBuffer stringBuffer = null;
		if(StringUtils.notEmpty(type) && "menu".equals(type)){
			stringBuffer = new StringBuffer("<ul>");
			if(menus != null && menus.size() > 0 ){
				for (int i = 0; i < menus.size(); i++) {
					if("zh_CN".equals(language)){
						stringBuffer.append("<li id='"+menus.get(i).getId()+"'><a href='#'>"+menus.get(i).getMenu_zh_name()+"</a></li>");
					}else{
						stringBuffer.append("<li id='"+menus.get(i).getId()+"'><a href='#'>"+menus.get(i).getMenu_en_name()+"</a></li>");
					}
				}
			} 
			stringBuffer.append("</ul>");
			jspWriter.print(stringBuffer.toString());
		}else{
			stringBuffer = new StringBuffer("<ul class='nav nav-second-level'>");
			if(menus != null && menus.size() > 0 ){
				for (int i = 0; i < menus.size(); i++) {
					if("zh_CN".equals(language)){
						stringBuffer.append("<li><a class=\"J_menuItem\" onclick=\"ajaxDiv('content', '"+menus.get(i).getUrl()+"')\">"+menus.get(i).getMenu_zh_name()+"</a></li>");
					}else{
						stringBuffer.append("<li><a class=\"J_menuItem\" onclick=\"ajaxDiv('content', '"+menus.get(i).getUrl()+"')\">"+menus.get(i).getMenu_en_name()+"</a></li>");
					}
					stringBuffer.append("<input type='hidden' name='menuID' value='"+ menus.get(i).getId() +"'/>");
				}
			}
			stringBuffer.append("</ul>");
			jspWriter.print(stringBuffer.toString());
		}
		
		return 0;
	}

}
